<?php


namespace app\operater\controller;

use think\Db;

class User extends Allow
{
    public function index(){
        $op_operation_id = session("op_operation_id");
        if($op_operation_id == 1){
            //查询出来所有用户
            $data = db("oprelationuser")
                ->order("id desc")
                ->paginate(10);

        }else{
            //查询出来当前运营人员的用户
            $data = db("oprelationuser")
                ->where("o_id",$op_operation_id)
                ->order("id desc")
                ->paginate(10);
        }
        $page = $data->render();
        $data = $data->toArray();
        $arr = [];
        foreach($data['data'] as $key=>$value){
//            dump($value);
            $arr[] = $value['u_id'];
        }
        unset($data['data']);
        $data['data'] = Db::table("xing_user")
            ->order("id desc")
            ->where("id","in",$arr)
            ->select();
        return view("index",[
            'data' => $data,
            'op_id' => $op_operation_id,
            'page' => $page
        ]);
    }
    public function moveUser()
    {
        if(request()->post()){
            $code = input("code");
            $old_code = input("old_code");
            $old_operation = db("operation")->where("code",$old_code)->find();
            $operation = db("operation")->where("code",$code)->find();
            if(empty($operation)) return errorMsg('运营号未查询到!!!');
            if(empty($old_operation)) return errorMsg('旧运营号未查询到!!!');
            $res = db("oprelationuser")->where('o_id',$old_operation['id'])->update(['o_id' => $operation['id']]);
            if(!$res) return errorMsg("迁移失败");
            return successMsg("迁移成功");
        }
        return view("move_user");
    }
}